package com.xhh.greynewsapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xhh.greynewsapp.R;
import com.xhh.greynewsapp.activity.NewsInfoActivity;
import com.xhh.greynewsapp.model.topnews.NewsData;

import java.util.List;

import butterknife.ButterKnife;

/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-26 15:38
 **/
public class NewsRecyclerViewAdapter extends RecyclerView.Adapter<NewsRecyclerViewAdapter.ViewHolder> {

    private static int newsMaxCount = 6;
    public static final int DEFAULT_TYPE = 0;
    public static final int REFRESH_TYPE = 1;

    private List<NewsData> dataList;
    private final String TAG = "NewsFragmentAdapter";
    private Context context;

    public NewsRecyclerViewAdapter(List<NewsData> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View defaultView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_news_item, parent, false);
        View refreshView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_touch_foot_refresh, parent, false);

        if (viewType == REFRESH_TYPE) {
            return new ViewHolder(refreshView, REFRESH_TYPE);
        } else {
            return new ViewHolder(defaultView, DEFAULT_TYPE);
        }

    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (getItemViewType(position) == REFRESH_TYPE) {
            holder.loading.setText("加载中...");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    newsMaxCount += 6;
                    notifyDataSetChanged();
                }
            }, 2000);

        } else {

            NewsData newsData = dataList.get(position);
            holder.tvNewsTitle.setText(newsData.getTitle());
            holder.tvNewsAuthorName.setText(newsData.getAuthor_name());

            if (newsData.getThumbnail_pic_s() != null) {
                Glide.with(holder.itemView).load(newsData.getThumbnail_pic_s()).into(holder.tvNewsPic1);
            }
            if (newsData.getThumbnail_pic_s02() != null) {
                Glide.with(holder.itemView).load(newsData.getThumbnail_pic_s02()).into(holder.tvNewsPic2);
            }
            if (newsData.getThumbnail_pic_s03() != null) {
                Glide.with(holder.itemView).load(newsData.getThumbnail_pic_s03()).into(holder.tvNewsPic3);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String url = newsData.getUrl();
                    Intent intent = new Intent(context, NewsInfoActivity.class);
                    intent.putExtra("URL", url);
                    context.startActivity(intent);
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        if (newsMaxCount > dataList.size()) {
            return dataList.size();
        }
        return newsMaxCount;

    }

    @Override
    public int getItemViewType(int position) {
        if (position == (newsMaxCount - 1)) {
            return REFRESH_TYPE;
        } else {
            return DEFAULT_TYPE;
        }

    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvNewsTitle;

        TextView tvNewsAuthorName;

        ImageView tvNewsPic1;

        ImageView tvNewsPic2;

        ImageView tvNewsPic3;

        TextView loading;

        public ViewHolder(@NonNull View itemView, int viewType) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            if (viewType == DEFAULT_TYPE) {
                tvNewsTitle = itemView.findViewById(R.id.news_title);
                tvNewsAuthorName = itemView.findViewById(R.id.news_author_name);
                tvNewsPic1 = itemView.findViewById(R.id.news_pic1);
                tvNewsPic2 = itemView.findViewById(R.id.news_pic2);
                tvNewsPic3 = itemView.findViewById(R.id.news_pic3);
            } else if (viewType == REFRESH_TYPE) {
                loading = itemView.findViewById(R.id.recycler_view_refresh_text);
            }
        }
    }
}
